package com.mf.cloud.controller;

import com.mf.cloud.entites.PayDTO;
import com.mf.cloud.resp.ResultData;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * ClassName: OrderController
 * Package: com.mf.cloud.controller
 *
 * @Author: lmf
 * @Create: 2024/3/10 11:45
 * @Version: 1.0
 * Description: 订单控制层
 */

@RestController  // 将 return 中的内容以 JSON字符串的形式返回客户端
@RequestMapping("/consumer/pay")
@Slf4j
@Tag(name = "用户支付")
public class OrderController {
    // 支付服务网址
//    public static final String PaymentSrv_URl = "http://localhost:8001/";
    // 使用服务注册中心上的微服务名称 避免写死
    public static final String PaymentSrv_URl = "http://cloud-payment-server";
    @Resource // 访问Rest服务的客户端
    private RestTemplate restTemplate;

    @Operation(summary = "新增")
    @PostMapping("/add")
    public ResultData<Integer> add(@RequestBody PayDTO pay){
        return restTemplate.postForObject(PaymentSrv_URl+"pay/add", pay, ResultData.class);

    }

    @Operation(summary = "删除支付订单")
    @GetMapping("/del/{id}")
    public ResultData<Integer> deleteOrder(@PathVariable("id") Integer id){
        System.out.println(id);
         return restTemplate.getForObject(PaymentSrv_URl+"pay/del/"+id,  ResultData.class,id);
    }

    @Operation(summary = "编辑支付订单")
    @PostMapping("/update")
    public ResultData<Integer> update(@RequestBody PayDTO pay){
        return restTemplate.postForObject(PaymentSrv_URl+"pay/update", pay, ResultData.class);

    }

    @Operation(summary = "根据id查询")
    @GetMapping("/select/{id}")
    public ResultData<PayDTO> selectById(@PathVariable("id") Integer id){

        return restTemplate.getForObject(PaymentSrv_URl+"pay/select/"+id,  ResultData.class,id);
    }




}
